package com.newland.starpos.partner.controller;

import com.newland.starpos.partner.bean.dto.UserLoginDTO;
import com.newland.starpos.partner.bean.dto.UserPwResetDTO;
import com.newland.starpos.partner.bean.dto.UserRegistDTO;
import com.newland.starpos.partner.controller.advice.BaseController;
import com.newland.starpos.partner.exception.CustomException;
import com.newland.starpos.partner.http.ResponseEnum;
import com.newland.starpos.partner.http.ResponseModel;
import com.newland.starpos.partner.service.UserService;
import com.newland.starpos.partner.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@GetMapping(value = "/phone/exist")
	public ResponseModel checkPhoneExist(String phoneNumber) throws CustomException {
		ResponseModel response = userService.checkPhoneExist(phoneNumber);
		return response;
	}

	@PostMapping(value = "/registration")
	public  ResponseModel registration(@RequestBody @Valid UserRegistDTO userRsgistDTO
										, BindingResult bind) throws CustomException {
		checkBindResult(bind);
		ResponseModel response = userService.register(userRsgistDTO);
		return response;
	}

	@PostMapping(value = "/login")
	public  ResponseModel registration(@RequestBody @Valid UserLoginDTO userLoginDTO,
			HttpServletRequest request, BindingResult bind) throws CustomException {

		checkBindResult(bind);
//		userLoginDTO.setMacAddress(request.getHeader("macAddress"));
		ResponseModel response = userService.login(userLoginDTO, request);
		return response;
	}
	
	@PostMapping(value = "/password/reset")
	public  ResponseModel resetPassword(@RequestBody UserPwResetDTO userPwResetDTO,
			HttpServletRequest request, BindingResult bind) throws CustomException {

		checkBindResult(bind);
		ResponseModel response = userService.resetPassword(userPwResetDTO, request);
		return response;
	}

	/**
	 * 应前端要求加入一个验证登录状态接口,能进入此方法说明已经通过accesstoken验证拦截器
	 * 直接返回验证通过即可
	 */
	@RequestMapping(value = "/status/login")
	public ResponseModel loginStatus() {
		return new ResponseModel(ResponseEnum.OK);
	}
}
